#ifndef FES_PLAYER_MGR_H
#define FES_PLAYER_MGR_H

#include <nel/misc/singleton.h>
#include <nel/net/service.h>
#include <game_share/object_pool.h>
#include <game_share/game_def.h>
/*
 * Keep a list of the players connected to that Frontend. Only map Id
 * to a Connection
 */
namespace FES
{
class CClient
{
public:
	enum ClientState
	{
		INIT,
		ONLINE,
		ONGAME
	};

	CClient() : uid(0), con(0), conZS(NLNET::TServiceId::InvalidId), State(INIT), ticks(0) { }
	~CClient() { uid = 0; con = 0; conZS = NLNET::TServiceId::InvalidId; State = INIT; ticks=0; }
	DEF::UID			uid;
	NLNET::TSockId		con;
	NLNET::TServiceId	conZS;
	ClientState			State;
	NLMISC::TTicks      ticks;

	void reset()	{	uid = 0; con = 0; conZS = NLNET::TServiceId::InvalidId; State = INIT;	}
};

class CClientMgr : public NLMISC::CSingleton<CClientMgr>
{
public:

	CClientMgr():_timeOut(5000),_preTicks(0),_updateInterval(0),_Clients(NULL),_preAsyncPlayerCount(0){}

	void init( NLNET::CCallbackServer* Clients );
	uint32 currClients();

	CClient* pushClient( DEF::UID uid, NLNET::TSockId from );

	CClient* findClient(DEF::UID userId);
	void removeClient(DEF::UID userId);

	void disconnect(DEF::UID userId);

	bool updateTime(DEF::UID userId);

	void update( NLMISC::TTicks tick );

private:

	uint32		            _clientMax;
	uint32		            _timeOut;
	uint32		            _updateInterval;
	uint32                  _asyncPlayerCount;
	NLMISC::TTicks		    _preTicks;
	NLMISC::TTicks          _preAsyncPlayerCount;
	NLNET::CCallbackServer* _Clients;

	typedef std::map<DEF::UID, CClient>    TClientMap;
	TClientMap                             _clientMap;
};

#define ClientMgr CClientMgr::instance()

}
#endif
